<?php
class M_qin_visits extends Cemvc_Db_MysqlDb{
  public $PrimaryKey='id';
  /*CEPHP会在要需要时查询主键，但主动设置主键可以使程序执行更快*/

  public function __construct()
  {
    parent::__construct();
  }

  /* insert a visit when register */
  public function add_visit($user_id, $visitor_id=0)
  {
    if (!$user_id)
      return false;
    
    return $this->insert(array("user_id"=>$user_id, "visitor"=>$visitor_id));
  }

  /** 
   * description: get visited times of thr $user_id
   * */
  public function get_visit($user_id)
  {
    return count($this->setField('id')->where("user_id = '$user_id'")->fetchAll());
  }

  /* get TOP 20 people which most visited */
  /* open: can be public/protect/private */
  public function get_hot_people($open='public')
  {
    $sql = "select u.id, u.gender, u.realname, u.figureurl, count(v.user_id) as num from visits v inner join users u on v.user_id=u.id where u.open='".$open."' group by v.user_id order by num desc limit ".HOT_PEOPLE_NUM.";";
    $this->mysqlQuery($sql);

    return $this->fetchAll();
  }

  /* get TOP 20 people in my family */
  public function get_hot_family_member($userid)
  {
    $sql = "select u.id, u.gender, u.realname, u.figureurl, count(v.user_id) as num from visits v inner join users u on v.user_id=u.id where u.creator=".$userid." group by v.user_id order by num desc limit ".HOT_FAMILY_MEMBER_NUM.";";
    $this->mysqlQuery($sql);

    return $this->fetchAll();
  }
}